/*
 * Copyright (c) 2018. paascloud.net All Rights Reserved.
 * 项目名称：paascloud快速搭建企业级分布式微服务平台
 * 类名称：RedisKeyUtil.java
 * 创建人：刘兆明
 * 联系方式：paascloud.net@gmail.com
 * 开源地址: https://github.com/paascloud
 * 博客地址: http://blog.paascloud.net
 * 项目官网: http://paascloud.net
 */

package com.csun.cmny.util;

import com.google.common.base.Preconditions;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;

/**
 * The class Redis key util.
 *
 * @author paascloud.net@gmail.com
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class RedisKeyUtil {

	/**
	 * The constant RESET_PWD_TOKEN_KEY.
	 */
	private static final String RESET_PWD_TOKEN_KEY = "cmny:restPwd";
	private static final String ACTIVE_USER = "cmny:activeUser";
	private static final String SEND_SMS_COUNT = "cmny:sms:count";
	private static final String SEND_EMAIL_CODE = "cmny:email:code";
	private static final String ACCESS_TOKEN = "cmny:token:accessToken";
	private static final String UPLOAD_FILE_SIZE = "cmny:file:upload_file_size";
	private static final String CSUN_SLEEP_DATA_FILE_PREFIX = "cmny:file:csun:sleep:file_prefix";
	private static final String CSUN_SLEEP_REPORT_FILE_PREFIX = "cmny:file:csun:sleep:report_prefix";
	private static final String CSUN_SLEEP_REPORT_TIME = "cmny:file:csun:sleep:report_time";
	private static final String CSUN_SLEEP = "cmny:device:csun:sleep";
	private static final String CSUN_BEDSIDE = "cmny:device:csun:bedside";
	private static final String CSUN_BUTTON = "cmny:device:csun:button";
	private static final String CSUN_GATEWAY = "cmny:device:csun:gateway";
	public static final String CSUN_GATEWAY_ONLINE = "cmny:device:csun:gateway:online";
	public static final String CSUN_BODYSENSOR_ONLINE = "cmny:device:csun:bodysensor:online";
	public static final String CSUN_SLEEP_ONLINE = "cmny:device:csun:sleep:online";
	public static final String CSUN_HUMIDISTAT_ONLINE = "cmny:device:csun:humidistat:online";
	public static final String CSUN_SLEEP_LEAVE_BED = "cmny:device:csun:sleep:leave:bed";
	public static final String CSUN_SLEEP_BREATH_ALERT = "cmny:device:csun:sleep:breath:alert";
	public static final String CSUN_SLEEP_HEARTBEAT_ALERT = "cmny:device:csun:sleep:heartbeat:alert";
	private static final String CSUN_GATEWAY_BUTTON = "cmny:device:csun:gateway:button:msg";
	private static final String CSUN_GATEWAY_BODY_ADC = "cmny:device:csun:gateway:body:adc:msg";
	private static final String CSUN_GATEWAY_BODY_SENSOR = "cmny:device:csun:gateway:body:sensor:msg";
	private static final String CSUN_GATEWAY_BODY_SENSOR_ROOM = "cmny:device:csun:gateway:body:sensor:room";
    public static final String CSUN_GATEWAY_BODY_SENSOR_GROUP_ONLINE = "cmny:device:csun:gateway:body:sensor:group:online";
	private static final String CSUN_BODY_SENSOR = "cmny:device:csun:button";
	private static final String SLEEP_CARE = "cmny:device:sleep:care";
	private static final String XX_WATCH = "cmny:device:xx:watch";
	private static final String RFID_LABEL = "cmny:device:rfid:label";
	private static final String ZN_PHONE = "cmny:device:zn:phone";
	private static final String CSUN_CAMERA = "cmny:device:csun:camera";
	private static final String CSUN_CAMERA_DOWN_SWITCH = "cmny:device:csun:camera:down:switch";
	public static final String CSUN_CAMERA_ONLINE = "cmny:device:csun:camera:online";
	private static final String CSUN_CAMERA_TASK = "cmny:device:csun:camera:task";
	private static final String CONTROLLER_CARD = "cmny:device:controller:card";
	private static final String OMC_OLDMAN_ID = "cmny:omc:oldman:id";
	private static final String OMC_BED_ID = "cmny:omc:bed:id";
	private static final String OMC_ROOM_ID = "cmny:omc:room:id";
	private static final String OMC_LIFE_ROOM_ID = "cmny:omc:life:room:id";
	private static final String OMC_BUILD_FLOOR = "cmny:omc:build:floor";
	private static final String OMC_GROUP_BUILD_FLOOR = "cmny:omc:group:build:floor";
	private static final String OMC_QUERY_MACHINE_WEATHER = "cmny:omc:weather";
	private static final String OMC_RECHARGE_SERIALNUMBER = "cmny:omc:recharge:serialnumber";
	private static final String UAC_GROUP_LANGUAGE_ID = "cmny:uac:group:language:id";
	private static final String DMC_ROOM_ALERT_SILENT_TIME = "cmny:dmc:room:silent";
	private static final String OMC_ROOM_CHECK_STAT = "cmny:omc:room:check:stat";
	private static final String UAC_GROUP_LOGINNAME = "uac:group:loginname";
	private static final String UAC_GROUP_NAME = "uac:group:name";
	private static final String UAC_USER_NAME = "uac:user:name";
	private static final String UAC_ADDRESS = "uac:address";
	private static final String UAC_MERCHANT = "uac:merchant";
	private static final String UAC_WEIXIN_SESSION_KEY = "uac:weixin:session_key";
	private static final String OMC_SPECIALIST="omc:specialist";
	private static final String OMC_REAL_TIME_ORDER_POSITIONING="omc:order:positioning";

	private static final int REF_NO_MAX_LENGTH = 100;

	/**
	 * 消息有效期 秒
	 */
	private static final long MSG_EXPIRE = 10 * 60;

	public static String getUacGroupName(String groupId) {
		return UAC_GROUP_NAME + ":" +groupId;
	}

	public static String getUacUserName(String userId) {
		return UAC_USER_NAME + ":" +userId;
	}

	public static String getUacAddress(String addressId) {
		return UAC_ADDRESS + ":" +addressId;
	}

	public static String getUacMerchant(String merchantId) {
		return UAC_MERCHANT + ":" +merchantId;
	}

	public static String getOmcSpecialist(String specialistId) {
		return OMC_SPECIALIST + ":" +specialistId;
	}

	public static String getOmcRealTimeOrderPositioning(String userId){return  OMC_REAL_TIME_ORDER_POSITIONING + ":" + userId ;}

	/**

	 * mqtt设备在线刷新时间 秒
	 */
	private static final long DEVICE_ONLINE_EXPIRE = 5 * 60;

	/**
	 * Gets reset pwd token key.
	 *
	 * @param resetPwdKey the rest pwd key
	 *
	 * @return the reset pwd token key
	 */
	public static String getResetPwdTokenKey(String resetPwdKey) {
		Preconditions.checkArgument(StringUtils.isNotEmpty(resetPwdKey), "参数不能为空");
		return RESET_PWD_TOKEN_KEY + ":" + resetPwdKey;

	}

	public static String getSendEmailCodeKey(String loginName, String email) {
		Preconditions.checkArgument(StringUtils.isNotEmpty(loginName), "Email不能为空");
		Preconditions.checkArgument(StringUtils.isNotEmpty(email), "用户名不能为空");
		return SEND_EMAIL_CODE + ":" + loginName + email;

	}

	/**
	 * Gets active user key.
	 *
	 * @param activeToken the active token
	 *
	 * @return the active user key
	 */
	public static String getActiveUserKey(String activeToken) {
		Preconditions.checkArgument(StringUtils.isNotEmpty(activeToken), "参数不能为空");
		return ACTIVE_USER + ":" + activeToken;

	}

	/**
	 * Gets send sms count key.
	 *
	 * @param ipAddr the ip addr
	 * @param type   mobile;ip;total
	 *
	 * @return the send sms count key
	 */
	public static String getSendSmsCountKey(String ipAddr, String type) {
		Preconditions.checkArgument(StringUtils.isNotEmpty(ipAddr), "请不要篡改IP地址");
		return SEND_SMS_COUNT + ":" + type + ":" + ipAddr;

	}


	/**
	 * Gets send sms rate key.
	 *
	 * @param ipAddr the ip addr
	 *
	 * @return the send sms rate key
	 */
	public static String getSendSmsRateKey(String ipAddr) {
		Preconditions.checkArgument(StringUtils.isNotEmpty(ipAddr), "请不要篡改IP地址");
		return SEND_SMS_COUNT + ":" + ipAddr;

	}

	public static String getAccessTokenKey(String token) {
		Preconditions.checkArgument(StringUtils.isNotEmpty(token), "非法请求token参数不存在");
		return ACCESS_TOKEN + ":" + token;
	}

	public static String createMqKey(String topic, String tag, String refNo, String body) {
		String temp = refNo;
		Preconditions.checkArgument(StringUtils.isNotEmpty(topic), "topic is null");
		Preconditions.checkArgument(StringUtils.isNotEmpty(tag), "tag is null");
		Preconditions.checkArgument(StringUtils.isNotEmpty(refNo), "refNo is null");
		Preconditions.checkArgument(StringUtils.isNotEmpty(body), "body is null");

		if (refNo.length() > REF_NO_MAX_LENGTH) {
			temp = StringUtils.substring(refNo, 0, REF_NO_MAX_LENGTH) + "...";
		}
		return topic + "_" + tag + "_" + temp + "-" + body.hashCode();
	}

	public static String getFileSizeKey() {
		return UPLOAD_FILE_SIZE;
	}

	public static String getCsunCameraKey(String deviceNo) {
		Preconditions.checkArgument(StringUtils.isNotEmpty(deviceNo), "设备编号不能为空");

		return CSUN_CAMERA + ":" + deviceNo;
	}

	public static String getCsunCameraDownSwitch() {

		return CSUN_CAMERA_DOWN_SWITCH;
	}

	public static String getCsunCameraOnline(String deviceNo) {

		Preconditions.checkArgument(StringUtils.isNotEmpty(deviceNo), "设备编号不能为空");

		return CSUN_CAMERA_ONLINE + ":" + deviceNo;
	}

	public static String getCsunCameraTask(String taskId) {

		Preconditions.checkArgument(StringUtils.isNotEmpty(taskId), "任务编号不能为空");

		return CSUN_CAMERA_TASK + ":" + taskId;
	}

	public static String getControllerCardKey(String deviceNo) {

		Preconditions.checkArgument(StringUtils.isNotEmpty(deviceNo), "设备编号不能为空");
		return CONTROLLER_CARD + ":" + deviceNo;
	}

	public static String getCsunSleepKey(String deviceNo) {
		Preconditions.checkArgument(StringUtils.isNotEmpty(deviceNo), "设备编号不能为空");
		return CSUN_SLEEP + ":" + deviceNo;
	}
	public static String getCsunBedsideKey(String deviceNo) {
		Preconditions.checkArgument(StringUtils.isNotEmpty(deviceNo), "设备编号不能为空");
		return CSUN_BEDSIDE + ":" + deviceNo;
	}

	public static String getCsunButtonKey(String deviceNo) {
		Preconditions.checkArgument(StringUtils.isNotEmpty(deviceNo), "设备编号不能为空");
		return CSUN_BUTTON + ":" + deviceNo;
	}

	public static String getCsunGatewayKey(String deviceNo) {
		Preconditions.checkArgument(StringUtils.isNotEmpty(deviceNo), "设备编号不能为空");
		return CSUN_GATEWAY + ":" + deviceNo;
	}

	public static String getCsunGateway(String deviceNo, String id) {
		Preconditions.checkArgument(StringUtils.isNotEmpty(deviceNo), "设备编号不能为空");
		return CSUN_GATEWAY + ":" + deviceNo + ":" + id;
	}

	public static String getCsunBodySensorKey(String deviceNo) {
		Preconditions.checkArgument(StringUtils.isNotEmpty(deviceNo), "设备编号不能为空");
		return CSUN_BODY_SENSOR + ":" + deviceNo;
	}

	public static String getSleepCareKey(String deviceNo) {
		Preconditions.checkArgument(StringUtils.isNotEmpty(deviceNo), "设备编号不能为空");
		return SLEEP_CARE + ":" + deviceNo;
	}

	public static String getXxWatchKey(String deviceNo) {
		Preconditions.checkArgument(StringUtils.isNotEmpty(deviceNo), "设备编号不能为空");
		return XX_WATCH + ":" + deviceNo;
	}

	public static String getRfidLabelKey(String deviceNo) {
		Preconditions.checkArgument(StringUtils.isNotEmpty(deviceNo), "设备编号不能为空");
		return RFID_LABEL + ":" + deviceNo;
	}

	public static String getZnPhoneKey(String deviceNo) {
		Preconditions.checkArgument(StringUtils.isNotEmpty(deviceNo), "设备编号不能为空");
		return ZN_PHONE + ":" + deviceNo;
	}

	public static String getOmcOldmanId(Integer oldmanId) {
		Preconditions.checkArgument(oldmanId != null, "老人Id不能为空");
		return OMC_OLDMAN_ID + ":" + oldmanId;
	}

	public static String getOmcBedId(Integer bedId) {
		Preconditions.checkArgument(bedId != null, "床位Id不能为空");
		return OMC_BED_ID + ":" + bedId;
	}

	public static String getOmcRoomId(Integer roomId) {
		Preconditions.checkArgument(roomId != null, "房间Id不能为空");
		return OMC_ROOM_ID + ":" + roomId;
	}

	public static String getOmcLifeRoomId(Integer roomId) {
		Preconditions.checkArgument(roomId != null, "房间Id不能为空");
		return OMC_LIFE_ROOM_ID + ":" + roomId;
	}

	public static String getOmcBuildFloor(Integer buildingId, Integer floor) {
		Preconditions.checkArgument(buildingId != null, "楼栋Id不能为空");
		Preconditions.checkArgument(floor != null, "楼层不能为空");

		return OMC_BUILD_FLOOR + ":building:" + buildingId + ":floor:" + floor;
	}

	public static String getOmGroupcBuildFloor(Long groupId ,Integer buildingId, Integer floor) {
		Preconditions.checkArgument(groupId != null, "机构Id不能为空");
		return OMC_GROUP_BUILD_FLOOR + ":group" + groupId + ":building:" + buildingId + ":floor:" + floor;
	}

	public static String getUacGroupLanguageId(Long groupId) {
		Preconditions.checkArgument(groupId != null, "机构Id不能为空");
		return UAC_GROUP_LANGUAGE_ID + ":" + groupId;
	}

	public static String getCsunGatewayBodyAdc(String deviceNo, String id) {

		return CSUN_GATEWAY_BODY_ADC + ":" + deviceNo + ":" + id;
	}

	public static String getCsunGatewayButton(String deviceNo, String id) {

		return CSUN_GATEWAY_BUTTON + ":" + deviceNo + ":" + id;
	}

	public static String getCsunGatewayBodySensor(String deviceNo, String id) {

		return CSUN_GATEWAY_BODY_SENSOR + ":" + deviceNo + ":" + id;
	}

	public static String getCsunGatewayOnline(String deviceNo) {

		return CSUN_GATEWAY_ONLINE + ":" + deviceNo;
	}

	public static String getCsunBodySensorOnline(String deviceNo) {

		return CSUN_BODYSENSOR_ONLINE + ":" + deviceNo;
	}

	public static String getCsunHumidistatOnline(String deviceNo) {

		return CSUN_HUMIDISTAT_ONLINE + ":" + deviceNo;
	}

	public static String getCsunSleepOnline(String deviceNo) {

		return CSUN_SLEEP_ONLINE + ":" + deviceNo;
	}

	public static String getCsunSleepLeaveBed(String deviceNo) {

		return CSUN_SLEEP_LEAVE_BED + ":" + deviceNo;
	}
	public static String getCsunSleepBreathAlert(String deviceNo) {

		return CSUN_SLEEP_BREATH_ALERT + ":" + deviceNo;
	}

	public static String getCsunSleepHeartbeatAlert(String deviceNo) {

		return CSUN_SLEEP_HEARTBEAT_ALERT + ":" + deviceNo;
	}

	public static String getOmcQueryMachineWeather(String todayDate){
		return OMC_QUERY_MACHINE_WEATHER + ":" + todayDate;
	}

	public static String getDmcRoomAlertSilentTime(Long roomId){
		return DMC_ROOM_ALERT_SILENT_TIME + ":" + roomId;
	}

	public static String getOmcRoomCheckStat(Long roomId) {
		return OMC_ROOM_CHECK_STAT + ":" + roomId;
	}

	public static long getMsgExpire() {

		return MSG_EXPIRE;
	}

	public static String getOmcRechargeSerialnumber(String date){
		return OMC_RECHARGE_SERIALNUMBER + ":" + date;
	}

	public static long getDeviceOnlineExpire() {

		return DEVICE_ONLINE_EXPIRE;
	}

	public static String getCsunSleepDataFilePrefix() {

		return CSUN_SLEEP_DATA_FILE_PREFIX;
	}

	public static String getCsunSleepReportFilePrefix() {

		return CSUN_SLEEP_REPORT_FILE_PREFIX;
	}

	public static String getCsunSleepReportTime() {

		return CSUN_SLEEP_REPORT_TIME;
	}

	public static String getCsunBodySensorIsInRoomKey(String deviceNo) {
		return CSUN_GATEWAY_BODY_SENSOR_ROOM +":"+ deviceNo;
	}

	public static String getCsunBodyGroupKey(Long id) {
		return CSUN_GATEWAY_BODY_SENSOR_GROUP_ONLINE +":"+ id;
	}

	public static String getUacGroupLoginname(String groupId) {
		Preconditions.checkArgument(StringUtils.isNotEmpty(groupId), "参数不能为空");
		return UAC_GROUP_LOGINNAME + ":" +groupId;
	}

	public static String getWeiXinSessionKey(String userId) {

		return UAC_WEIXIN_SESSION_KEY + ":" + userId;
	}
}
